package com.xy.farm.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.xy.farm.R;
import com.xy.farm.activity.base.BaseActivity;
import com.xy.farm.activity.fragment.DealFragment;
import com.xy.farm.activity.fragment.DynamicFragment;
import com.xy.farm.activity.fragment.FarmFragment;
import com.xy.farm.activity.fragment.MyFragment;
import com.xy.farm.activity.fragment.NewsFragment;

public class MainActivity extends BaseActivity
{
    private int index = 2;
    private Fragment[] fragments;
    private TextView[] textViews;
    private ImageView[] imageViews;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_deal).setOnClickListener(this);
        findViewById(R.id.btn_dynamic).setOnClickListener(this);
        findViewById(R.id.btn_farm).setOnClickListener(this);
        findViewById(R.id.btn_news).setOnClickListener(this);
        findViewById(R.id.btn_my).setOnClickListener(this);
        fragments = new Fragment[]{new DealFragment(), new DynamicFragment(), new FarmFragment(), new NewsFragment(), new MyFragment()};
        textViews = new TextView[]{findViewById(R.id.tv_deal), findViewById(R.id.tv_dynamic), findViewById(R.id.tv_farm), findViewById(R.id.tv_news), findViewById(R.id.tv_my)};
        imageViews = new ImageView[]{findViewById(R.id.iv_deal), findViewById(R.id.iv_dynamic), findViewById(R.id.iv_farm), findViewById(R.id.iv_news), findViewById(R.id.iv_my)};
        if (!getIntent().hasExtra("page"))
            setFragment(2);
        else
            setFragment(getIntent().getIntExtra("page", 2));

    }

    @Override
    public void onClick(View v)
    {
        super.onClick(v);
        switch (v.getId())
        {
            case R.id.btn_deal:
                setFragment(0);
                break;
            case R.id.btn_dynamic:
                setFragment(1);
                break;
            case R.id.btn_farm:
                setFragment(2);
                break;
            case R.id.btn_news:
                setFragment(3);
                break;
            case R.id.btn_my:
                setFragment(4);
                break;
        }
    }

    @Override
    protected void onNewIntent(Intent intent)
    {
        super.onNewIntent(intent);
        if (getIntent().hasExtra("page"))
            setFragment(getIntent().getIntExtra("page", 2));
    }
    private void setFragment(int position)
    {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (!fragments[position].isAdded())
            transaction.add(R.id.frame_layout, fragments[position]);
        if (index != position)
        {
            textViews[index].setSelected(false);
            imageViews[index].setSelected(false);
            transaction.hide(fragments[index]);
        }
        textViews[position].setSelected(true);
        imageViews[position].setSelected(true);
        transaction.show(fragments[position]);
        transaction.commit();
        this.index = position;
    }
}